/**
 * 
 */
package com.jie.eclipse.pan.utils.gef;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.SimpleFactory;

import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntities;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;
import com.jie.eclipse.pan.gef.entry.PanPaletteTemplateEntry;
import com.jie.eclipse.pan.utils.Utils;

/**
 * @author yanjie
 * 
 */
public class PaletteFactory {
	private  static PaletteFactory instance = null ;
	public static PaletteFactory getInstance(){
		if(instance == null){
			instance = new PaletteFactory();
		}
		return instance;
	}
	
	private PaletteContainer createControlGroup(PaletteRoot root) {
		PaletteGroup controlGroup = new PaletteGroup("Control Group");

		List entries = new ArrayList();
		ToolEntry tool = new SelectionToolEntry();
		entries.add(tool);
		root.setDefaultEntry(tool);
		tool = new ConnectionCreationToolEntry("Connection",
				"Create a connection", null, null, /* small icon */
				null /* large icon */
		);
		entries.add(tool);		

		controlGroup.addAll(entries);
		return controlGroup;
	}

	private PaletteContainer createTagDrawer(BaseDataEntity data) {
		PanImageHelper imgHelper = PanImageHelper.getInstance();
		String type_text = data.gpsv("text");
		String type = data.gpsv("value");
		if("custom".equalsIgnoreCase(type)){
			type_text = type_text+"("+QMDao.getInstance().getCurUserName()+")";
		}
		
		PaletteDrawer drawer = new PaletteDrawer(type_text, imgHelper.getImageDescriptor("icons/tag/"+data.gpsv("icon")));
		BaseDataEntities rows = QMDao.getInstance().getPanTags(type);
		List entries = new ArrayList();
		if(rows != null && rows.size()>0){
			//CombinedTemplateCreationEntry
			for (int i = 0; i < rows.size(); i++) {
				BaseDataEntity row = rows.get(i);
				PanPaletteTemplateEntry entry = new PanPaletteTemplateEntry(
						row.gpsv("name"),//顯示名稱
						row.gpsv("rem")+".", //描述信息
						row,
						row.isNullOrEmpty("icon") ? null : imgHelper.getImageDescriptor("icons/tag/"+row.gpsv("icon")),
						null);
				entries.add(entry);
			}
		}
		if("Y".equalsIgnoreCase(data.gpsv("expand"))){
			drawer.setInitialState(PaletteDrawer.INITIAL_STATE_OPEN);
		}else{
			drawer.setInitialState(PaletteDrawer.INITIAL_STATE_CLOSED);
		}
		drawer.addAll(entries);
		return drawer;
	}
	
	private List createCategories(PaletteRoot root) {
		List categories = new ArrayList();
		BaseDataEntities rows = QMDao.getInstance().getPanTagTypes();
		for (int i = 0; i < rows.size(); i++) {
			categories.add(createTagDrawer(rows.get(i)));
		}
		return categories;
	}

	public PaletteRoot createPalette() {
		PaletteRoot paletteRoot = new PaletteRoot();
		paletteRoot.addAll(createCategories(paletteRoot));
		return paletteRoot;
	}
}
